package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGamePatrol.*;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.commondata.patrol.PatrolLocation;
import com.motu.monstercity.server.game.commondata.patrol.PatrolTable;
import com.motu.monstercity.server.game.commondata.system.UserExp;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.Tool;

public class PatrolHandler {

    // CMD_GAME_PATROL_BEGIN = 71501; // 开始巡查
    public static Object CMD_GAME_PATROL_BEGIN(RequestParam clientParam) throws Exception {
        CSGamePatrolBeginRequest request = CSGamePatrolBeginRequest.parseFrom(clientParam.getData());
        long userId = clientParam.getUserId();
        UserInfo userInfo = GameUser.getUserInfo(userId);
//        if (!GameDataManager.checkModuleOpen(userInfo, SystemOpen.PATROL)) { // 判断等级是否足够开启巡查功能
//            return ErrorWord.LEVEL_LACK;
//        }
        int num = request.getNum();
        if (num < 1) {
            return ErrorWord.BAD_PARAM; // 参数错误
        }
//        if (num == 10) { // 判断等级是否足够开启十连巡查
//            if (!GameDataManager.checkModuleOpen(userInfo, SystemOpen.PATROL10)) {
//                return ErrorWord.LEVEL_LACK;
//            }
//        }
        UserExp userExp = GameDataManager.getUserExp(userInfo.getLevel());
        if (num > userExp.getTravelNum()) {
            return ErrorWord.BAD_PARAM;
        }
        int costItemId = 0;
        UserPatrol userPatrol = PatrolManager.getUserPatrol(userInfo.getId());
        if (request.getSecretaryId() > 0) { // 指定秘书
            if (userPatrol.getNum() < AllParam.PATROL_LOCATION_PERFORCE_EVENT.length) {
                return ErrorWord.INVALID_OPERATION; // 引导巡查不能指定秘书id
            }
            costItemId = ItemId.CLUE;
        } else { // 随机秘书
            costItemId = ItemId.ENERGY;
        }
        long itemNum = BagManager.getUserItemNum(userInfo, costItemId);
        if (itemNum < num) {
            return ErrorWord.ITEM_LACK;
        }
        CSGamePatrolBeginResponse.Builder response = CSGamePatrolBeginResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        UserPower userPower = GameUser.getUserPower(userId);
        UserAffair userAffair = GameUser.getUserAffair(userId);
        RewardManager.subReward(userInfo, costItemId, num, pbUserData, LogType.PATROL_BEGIN);
        for (int i = 0; i < num; i++) {
            PatrolTable patrolTable = null;
            if (request.getSecretaryId() > 0) {
                patrolTable = PatrolManager.getPatrolTableBySecretaryId(request.getSecretaryId());
            } else {
                patrolTable = PatrolManager.randomPatrolTable(userId);
            }
            int eventId = 0;
            if (userPatrol.getNum() < AllParam.PATROL_LOCATION_PERFORCE_EVENT.length) {
                eventId = AllParam.PATROL_LOCATION_PERFORCE_EVENT[userPatrol.getNum()];
            }
            if (eventId > 0) {
                PatrolLocation location = PatrolManager.getPatrolLocation(eventId);
                if (location == null) {
                    return ErrorWord.NOT_DATA;
                }
                int[] secretaryReward = location.getSecretaryRewardArray();
                if (secretaryReward != null && secretaryReward.length > 0) {
                    int secretaryId = secretaryReward[0];
                    patrolTable = PatrolManager.getPatrolTableBySecretaryId(secretaryId);
                }
            }
            if (eventId == 0) {
                int[] eventIds, eventWeights;
                UserSecretary userSecretary = SecretaryManager.getUserSecretary(userId, patrolTable.getSecretaryId());
                if (userSecretary == null) { // 如果未获得该秘书就取事件1的权重
                    eventIds = patrolTable.getEventIdNotArray();
                    eventWeights = patrolTable.getEventWeightNotArray();
                } else { // 如果已有该秘书则取事件2的权重
                    eventIds = patrolTable.getEventIdObtainedArray();
                    eventWeights = patrolTable.getEventWeightObtainedArray();
                }
                // 根据权重算出随机事件
                int randomIndex = Tool.getWeightIndex(eventWeights);
                eventId = eventIds[randomIndex];
            }
            response.addPatrolEventList(eventId);
            PatrolLocation location = PatrolManager.getPatrolLocation(eventId);
            if (location == null) {
                return ErrorWord.NOT_DATA;
            }
            if (location.getEventType() == PatrolLocation.EVENT_TYPE_DIALOG) { // 普通对话事件
                PatrolManager.addSecretaryPatrol(userInfo, userPower, userAffair, location, patrolTable, pbUserData);
            }
            userPatrol.addNum(1);
            RewardManager.addReward(userInfo, location.getReward(), pbUserData, LogType.PATROL_BEGIN, eventId);
        }
        userPatrol.update();
        ProtoDataUtils.updatePBUserData(response.getUserBuilder(), userPatrol);
        MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_159, num);
        MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_128, num);
        MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_9, num);
        RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_9, num);
        RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_159, num);
        RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_128, num);
        return response;
    }

    // CMD_GAME_PATROL_ADD_ENERGY = 71502; // 增加巡查体力
    public static Object CMD_GAME_PATROL_ADD_ENERGY(RequestParam clientParam) throws Exception {
        CSGamePatrolAddEnergyRequest request = CSGamePatrolAddEnergyRequest.parseFrom(clientParam.getData());
        UserInfo userInfo = GameUser.getUserInfo(clientParam.getUserId());
        int num = request.getNum();
        if (num < 1 || num > 999) {
            return ErrorWord.BAD_PARAM;
        }
        long itemNum = BagManager.getUserItemNum(userInfo, ItemId.PATROL_ENERGY);
        if (itemNum < num) {
            return ErrorWord.ITEM_LACK;
        }
        CSGamePatrolAddEnergyResponse.Builder response = CSGamePatrolAddEnergyResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        RewardManager.subReward(userInfo, ItemId.PATROL_ENERGY, num, pbUserData, LogType.PATROL_ADD_ENERGY);
        RewardManager.addReward(userInfo, ItemId.ENERGY, num, pbUserData, LogType.PATROL_ADD_ENERGY);
        return response;
    }

    // CMD_GAME_PATROL_MAIN_INFO = 71503; // [1]第一次请求
    public static Object CMD_GAME_PATROL_MAIN_INFO(RequestParam clientParam) throws Exception {
        CSGamePatrolMainInfoRequest request = CSGamePatrolMainInfoRequest.parseFrom(clientParam.getData());
        CSGamePatrolMainInfoResponse.Builder response = CSGamePatrolMainInfoResponse.newBuilder();

        long userId = clientParam.getUserId();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        UserPatrol userPatrol = PatrolManager.getUserPatrol(userId);
        ProtoDataUtils.updatePBUserData(pbUserData, userPatrol, true);
        return response;
    }

}
